package twentytwenty;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-10 12:43
 * @Modified By:
 */
public class SummaryRanges_228 {
    public static void main(String[] args) {
        int [] nums = new int[]{0,2,3,4,6,8,9};
        System.out.println(summaryRanges(nums));
    }

    public static List<String> summaryRanges(int [] nums){
        List<String> result = new ArrayList<>();
        int len = nums.length;
        if (len == 0){
            return result;
        }
        if (len == 1){
            result.add(nums[0] + "");
            return result;
        }
        int start = 0;
        while (start < len){
            int end = findEnd(start, len - 1, nums);
            if(start == end){
                result.add(nums[end]+"");
            }else {
                result.add(nums[start] + "->" + nums[end]);
            }
            start = end + 1;
        }
        return result;
    }

    public static int findEnd(int start,int end,int [] nums){
        int len = nums.length;
        for (int i = start; i < len - 1; i++) {
            end = i;
            if (nums[i + 1] - nums[i] == 1){
                end = i + 1;
            }else {
                break;
            }
        }
        return end;
    }
}
